package com.eilieili.lyq.supervideo;

import java.util.HashMap;

public class FragmentManagerWrapper {

    private volatile static FragmentManagerWrapper mInstance = null;

    public static FragmentManagerWrapper getInstance(){
        if(mInstance == null){
            synchronized (FragmentManagerWrapper.class){
                if(mInstance == null){
                    mInstance = new FragmentManagerWrapper();
                }
            }
        }
        return mInstance;
    }

    private HashMap<String,BaseFragment> mHashMap = new HashMap<>();

    public BaseFragment createFragment(Class<?> clazz,boolean isObtain){
        BaseFragment resultFragment = null;
        String className = clazz.getName();
        if(mHashMap.containsKey(className)){
            resultFragment = mHashMap.get(className);
        }else{
            try {
                resultFragment = (BaseFragment) Class.forName(className).newInstance();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InstantiationException e) {
                e.printStackTrace();
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
        }
        if(isObtain){
            mHashMap.put(className,resultFragment);
        }
        return resultFragment;
    }

}
